//
//  UIButton+Extension.swift
//  weibo
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit


extension UIButton {
//便利但构造函数 实例化UIButton
    //实例化一个 有背景视图 + title 的自定义按钮
    convenience init(title:String,backImageName:String?,fontSize:CGFloat,color:UIColor,imageName: String? = nil){
       //需要调用本类的制定构造函数 实例化自己
        self.init()
        if imageName != nil {
        setImage(UIImage(named: imageName!), forState: .Normal)
        }
        if backImageName != nil {
        
            setBackgroundImage(UIImage(named:backImageName!), forState: .Normal)
        }
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        sizeToFit()
    }
    //返回一个  有背景视图 + image 按钮的样式
    convenience init(imageName:String,backImageName:String) {
    
        self.init()
        setImage(UIImage(named:imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        //根据背景视图大小 自动设置大小
        sizeToFit()
    }
}